﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace ARchGL.Platform.WindowsService.Application
{
    /// <summary>
    /// LED 服务基类
    /// </summary>
    public class LEDBase
    {
        #region EQ2008_Dll 基础方法

        //==========================1、节目操作函数======================//
        /// <summary>添加节目</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddProgram(int cardNumber, bool bWaitToEnd, int iPlayTime);
        /// <summary>删除所有节目</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_DelAllProgram(int cardNumber);

        /// <summary>添加单行文本区</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddSingleText(int cardNumber, ref User_SingleText pSingleText, int iProgramIndex);
        /// <summary>添加文本区</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddText(int cardNumber, ref User_Text pText, int iProgramIndex);
        //添加时间区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddTime(int cardNumber, ref User_DateTime pdateTime, int iProgramIndex);
        //添加图文区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddBmpZone(int cardNumber, ref User_Bmp pBmp, int iProgramIndex);
        //指定图像句柄添加图片
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_AddBmp(int cardNumber, int iBmpPartNum, IntPtr hBitmap, ref User_MoveSet pMoveSet, int iProgramIndex);
        //指定图像路径添加图片
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_AddBmpFile(int cardNumber, int iBmpPartNum, string strFileName, ref User_MoveSet pMoveSet, int iProgramIndex);


        //添加计时区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddTimeCount(int cardNumber, ref User_Timer pTimeCount, int iProgramIndex);
        //添加温度区
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern int User_AddTemperature(int cardNumber, ref User_Temperature pTemperature, int iProgramIndex);

        /// <summary>发送数据</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_SendToScreen(int cardNumber);
        //====================================================================//       

        //=======================2、实时发送数据（高频率发送）=================//
        /// <summary>实时建立连接</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_RealtimeConnect(int cardNumber);
        //实时发送图片数据
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_RealtimeSendData(int cardNumber, int x, int y, int iWidth, int iHeight, IntPtr hBitmap);
        //实时发送图片文件
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_RealtimeSendBmpData(int cardNumber, int x, int y, int iWidth, int iHeight, string strFileName);
        /// <summary>实时发送文本</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_RealtimeSendText(int cardNumber, int x, int y, int iWidth, int iHeight, string strText, ref User_FontSet pFontInfo);
        /// <summary>实时关闭连接</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_RealtimeDisConnect(int cardNumber);
        /// <summary>实时发送清屏</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_RealtimeScreenClear(int cardNumber);
        //====================================================================//

        //==========================3、显示屏控制函数组=======================//
        /// <summary>校正时间</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_AdjustTime(int cardNumber);
        //开屏
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_OpenScreen(int cardNumber);
        //关屏
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_CloseScreen(int cardNumber);
        /// <summary>亮度调节</summary>
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern bool User_SetScreenLight(int cardNumber, int iLightDegreen);
        //Reload参数文件
        [DllImport("EQ2008_Dll.dll", CharSet = CharSet.Ansi)]
        public static extern void User_ReloadIniFile(string strEQ2008_Dll_Set_Path);
        //====================================================================//

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);

        public static int cardNumber = 1;      //控制卡地址   g_iCardNum
        public static int g_iGreen = 0xFF00; //绿色
        public static int g_iYellow = 0xFFFF; //黄色
        public static int g_iRed = 0x00FF; //红色
        public static int g_iProgramIndex = 0;
        public static int g_iProgramCount = 0;

        public static int width = 0;//屏幕宽度
        public static int heigh = 0;//屏幕高度
        public static string SectionNumber = "";//标段编号，后端根据编号动态返回名称

        #endregion

        public LEDBase()
        {
            //width = 96;
            //heigh = 48;
            LoadInit();//加载宽高信息
            User_AdjustTime(cardNumber);
        }

        /// <summary>获取屏幕</summary>
        public int GetScreen()
        {
            if (width == 96 && heigh == 48)//测试屏幕
                return 0;
            if (width == 450 && heigh == 48)//华汇现场
                return 1;

            return 0;
        }

        /// <summary>断开连接</summary>
        public void DisposeConnect()
        {
            try
            {
                User_RealtimeDisConnect(cardNumber);
            }
            catch { }
            //try
            //{
            //    User_RealtimeDisConnect(cardNumber);
            //}
            //catch { }
            //try
            //{
            //    User_RealtimeDisConnect(cardNumber);
            //}
            //catch { }
        }

        /// <summary>加载宽高信息</summary>
        private void LoadInit()
        {
            var lines = File.ReadAllLines("./EQ2008_Dll_Set.ini", Encoding.UTF8);

            foreach (var item in lines)
            {
                if (item.Contains(" ScreemWidth="))
                    width = int.Parse(item.Split('=')[1].Trim());
                if (item.Contains(" ScreemHeight="))
                    heigh = int.Parse(item.Split('=')[1].Trim());
                if (item.Contains(" SectionNumber="))
                    SectionNumber = item.Split('=')[1].Trim();

                if (width > 0 && heigh > 0 && !string.IsNullOrEmpty(SectionNumber)) break;
            }
        }
    }
}
